package com.example.springbootmqdemo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TtlRabbitMqConfiguration {

    //1.声明一个交换机
    @Bean
    public DirectExchange ttldirectExchange() {
        return new DirectExchange("ttl_direct_exchange", true, false);
    }

    //2. quque ttl队列
    @Bean
    public Queue ttlQueue() {
        Map<String,Object> args = new HashMap<>();
        args.put("x-message-ttl",5000); //5s过期
        args.put("x-dead-letter-exchange","dead_direct_exchange"); //5s过期后 消息转发到 dead_direct_exchange交换机
        args.put("x-dead-letter-routing-key","dead"); //设置 dead_direct_exchange交换机的 routKey 为配置类中的”dead“
//        args.put("x-max-length",5); //设置 ttl交换机的最多5个  新增参数时要删除queue 重启不然会报错
        return new Queue("ttl.direct.queue", true,false,false,args);
    }

    //2. message ttl队列
    @Bean
    public Queue ttlMsgQueue() {
        return new Queue("ttl.direct.message.queue", true,false,false);
    }


    @Bean
    //3.1  将队列ttl.direct.queue 绑定到 名为 “ttl_order_exchange”的direct类型的交换机上 routKey=“ttl”
    public Binding emallBinding2() {
        return BindingBuilder.bind(ttlQueue()).to(ttldirectExchange()).with("ttl");
    }


    @Bean
    //3.1  将队列ttl.direct.message.queue 绑定到 名为 “ttl_order_exchange”的direct类型的交换机上 routKey=“ttlmessage”
    public Binding emallBindingMsg2() {
        return BindingBuilder.bind(ttlMsgQueue()).to(ttldirectExchange()).with("ttlmessage");
    }

}
